//
//  ViewController.m
//  WebViewCache
//
//  Created by yxl on 2022/3/30.
//

#import "ViewController.h"
#import <WebKit/WKWebView.h>
#import "LCWebViewCache.h"


@interface ViewController ()<WKNavigationDelegate,WKUIDelegate>
@property (nonatomic,strong) WKWebView *webView;
@property (nonatomic,copy) NSString *url;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.url = @"https://www.jianshu.com/u/3715407414e6";
    self.webView.frame = self.view.bounds;
    [self.view addSubview:self.webView];
    [self startLoadRequest];
}

- (void)startLoadRequest {
    self.url = [self.url stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
    if ([self.url hasPrefix:@"http://"] || [self.url hasPrefix:@"https://"] || [self.url containsString:@"www"]) {
        NSURLRequest *request = [[NSURLRequest alloc] initWithURL:[NSURL URLWithString:[self.url stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]]]cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60];
        [self.webView loadRequest:request];
    } else {
        [self.webView loadHTMLString:self.url baseURL:nil];
    }
}

#pragma mark - Get
- (WKWebView *)webView {
    if (!_webView) {
        _webView = [LCWebViewCache getWKWebViewFromPool];
        _webView.navigationDelegate = self;
        _webView.UIDelegate = self;
    }
    return _webView;
}


@end
